package common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ever
 */
public class Main {

    public static void main(String[] args) {

        ReadConf readConf = new ReadConf();

        List<Component> srcList = readConf.getSrcList();

        List<String> urlList = UrlList.getUrlList();

        List<FileOutPut> list = new ArrayList<FileOutPut>(urlList.size());

        String outPutPath;
        for (String path : urlList) {
            for (Component ce : srcList) {
                if (path.matches("^(" + ce.getSourcePath() + ").+")) {

                    if (path.endsWith(".java")) {
                        path = path.replace(".java", ".class");
                    }
                    path = path.replaceAll("^(" + ce.getSourcePath() + ")", StringUtils.isBlank(ce.getDeployPath()) ? readConf.getOutput() : ce.getDeployPath());
                }
            }
            outPutPath = path;
            if (path.startsWith(readConf.webRoot)) {
                outPutPath = outPutPath.replaceAll(readConf.webRoot, "");
            }
            outPutPath = readConf.getOutPutPath() + File.separator + readConf.getProName() + File.separator + outPutPath;
            path = Constant.workSpacePath + File.separator + path;
            path = path.replaceAll("([\\\\/]{1})", File.separator + File.separator);
            outPutPath = outPutPath.replaceAll("([\\\\/]{1})", File.separator + File.separator);

            list.add(new FileOutPut(path, outPutPath));
            System.out.println(outPutPath);
        }

        File proOutput = new File(readConf.getOutPutPath() + File.separator + readConf.getProName());
        if (proOutput.exists()) {
            proOutput.delete();
        }


        Main main = new Main();
        for (FileOutPut fileOutPut : list) {
            main.copyFileByUrl(fileOutPut);
        }
        System.out.println("打包完成");
    }

    public void copyFileByUrl(FileOutPut fileOutPut) {
        File baseFile = new File(fileOutPut.getClassPath());
        File outPutFile = new File(fileOutPut.getOutPutPath());

        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            if (baseFile.exists() && baseFile.isFile()) {

                if (!outPutFile.exists()) {
                    File dir = new File(outPutFile.getParent());
                    if (!dir.exists()) {
                        dir.mkdirs();
                    }
                    outPutFile.createNewFile();
                }
                // 复制文件
                fis = new FileInputStream(fileOutPut.getClassPath());
                fos = new FileOutputStream(fileOutPut.getOutPutPath());
                int in = fis.read();
                while (in != -1) {
                    fos.write(in);
                    in = fis.read();
                }
            } else if (baseFile.isDirectory()) {
                File[] files = baseFile.listFiles();
                for (File file : files) {
                    FileOutPut fop = new FileOutPut(baseFile.getPath() + File.separator + file.getName(),
                            outPutFile.getPath() + File.separator + file.getName());
                    this.copyFileByUrl(fop);
                }
            } else {
                System.out.println("文件不存在：" + fileOutPut.getClassPath());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            try {
                if (fos != null) {
                    fos.close();
                }
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
